const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const { VueLoaderPlugin } = require("vue-loader");
const { DefinePlugin, Generator } = require("webpack");
const resolvePath = (_path) => path.resolve(__dirname, _path);
const CopyPlugin = require("copy-webpack-plugin");

//const CesiumSource = "../node_modules/cesium/Build/Cesium/";
const CesiumRunPath = "public/lib/cesium/";

const my_externals = {
  "axios":"axios",
  "vue": "Vue",
  "vue-router": "VueRouter",
  "element-plus": "ElementPlus",
  "cesium": "Cesium",
};

const CDN = {
  css:[
    "public/lib/element-plus/dist/index.css",
    "public/lib/cesium/Build/Cesium/Widgets/widgets.css",
  ],
  js:[
    "public/lib/vue/dist/vue.global.js",
    "public/lib/axios/dist/axios.js",
    "public/lib/element-plus/dist/index.full.js",
    "public/lib/cesium/Build/Cesium/Cesium.js",
    "public/lib/vue-router/dist/vue-router.global.js"
  ]
};

module.exports = {
  mode: "development",
  entry: "./src/index.js",
  output: {
    path: resolvePath("../dist"),
    filename: "public/js/[contenthash].js",
    clean: true,
    publicPath: '/',
    sourcePrefix: "",
  },
  resolve: {
    alias: {
      "@": resolvePath("../src"),
    },
    extensions: [".js", ".vue", ".ts", ".tsx"],
  },
  externals: my_externals,
  amd: {
    toUrlUndefined: true,
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: resolvePath("../public/index.html"),
      filename: "index.html",
      cdn: CDN
    }),
    new DefinePlugin({
      __VUE_OPTIONS_API__: true,
      __VUE_PROD_DEVTOOLS__: true,
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true,
      CESIUM_BASE_URL: JSON.stringify(CesiumRunPath)
    }),
    new CopyPlugin({
      patterns: [
        /*
        { from: resolvePath(CesiumSource + "Assets"), to: CesiumRunPath + "Assets" },
        { from: resolvePath(CesiumSource + "ThirdParty"), to: CesiumRunPath + "ThirdParty" },
        { from: resolvePath(CesiumSource + "Workers"), to: CesiumRunPath + "Workers" },
        { from: resolvePath(CesiumSource + "Widgets"), to: CesiumRunPath + "Widgets" },
         */
        { from: resolvePath("../public/images"), to: "public/images" },
        { from: resolvePath("../public/config"), to: "public/config" },
        { from: resolvePath("../public/lib"), to: "public/lib" },
      ]
    })

  ],
  module: {
    rules: [
      {
        test: /\.vue$/i,
        use: "vue-loader",
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.less$/i,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /\.(bmp|png|jpg)/i,
        type: "asset/resource",
        generator: {
          filename: "public/images/[contecthash][ext]"
        }
      }
    ],
    unknownContextCritical: false,
    unknownContextRegExp: /\/cesium\/cesium\/Source\/Core\/buildModuleUrl\.js/,
  },
  devtool: "inline-source-map",
  devServer: {
    port: 80,
    hot: true,
    open: true,
    historyApiFallback: true
  },
};
